//
// Created by JYL on 2024-06-22.
/* 平衡小车的逻辑应用代码：瞄准于电赛F题送药小车 */
#include "app.h"
#include "beep.h"

APP app;
CarAction action;

//转弯前剩余路口数
//uint8_t Num1[]={Wait,Line,L_90,Line,T_180,Line,R_90,Line,T_180};
uint8_t NumMap[9][10]={
         {Wait},//省略数字0
         //近端
         {L_90,T_180,R_90,T_180},     //数字1
         {R_90,T_180,L_90,T_180},     //数字2
         //中端
         {L_90,T_180,R_90,T_180},     //数字3
         {L_90,T_180,R_90,T_180},     //数字4
         //远端
         {L_90,T_180,R_90,T_180},     //数字5
         {L_90,T_180,R_90,T_180},     //数字6
         {L_90,T_180,R_90,T_180},     //数字7
         {L_90,T_180,R_90,T_180}    //数字8
};
//状态机写法，小车行为比较复杂，整个行程需要抽象成状态机
void Application(uint8_t state)
{
    //TODO 判断转换规则，获取事件标志组
    osSemaphoreAcquire(myBinarySem_carHandle,5);
    if(app.Control_flag==AUTO){
        switch (state) {
            case Wait:
                if(osEventFlagsWait(myEvent_AppHandle,EVENT_UART_SEND_NUM,pdFALSE,0)==osOK){
                    app.state=Line;
                }break;
                //大部分的状态转换由串口来控制(osEventFlagsWait 寻找路口，找到之后开始定时)
                //Line里面需要查询下一个行为？还是留空
            case Line:
                if(osEventFlagsWait(myEvent_AppHandle,EVENT_K210_FIND_TURN,pdTRUE,0)==osOK){
                    //查找当前状态机转换数组
                    app.Turn_mode=90;
                }
                if(osEventFlagsWait(myEvent_AppHandle,EVENT_K210_FIND_END,pdTRUE,0)==osOK){
                    app.Turn_mode=180;
                }
                break;
            case Cross:osTimerStart(myTimer_carHandle,CROSS_T_DELAY);break;
            case Corner:osTimerStart(myTimer_carHandle,CORNER_N_DELAY);break;
            case L_90:Flag_front=0;Flag_Left=1;
                osTimerStart(myTimer_carHandle,TURN_90_DELAY);break;
            case R_90:Flag_front=0;Flag_Right=1;
                osTimerStart(myTimer_carHandle,TURN_90_DELAY);break;
            case T_180:Flag_back=Flag_Left=Flag_front=0;Flag_Right=1;
                osTimerStart(myTimer_carHandle,TURN_90_DELAY*2);break;

        }
    }else if(app.Control_flag==MANUAL){//手动控制的测试模式
        switch (state) {    //
            case Wait:Flag_Right=Flag_back=Flag_Left=Flag_front=0;break;
            case L_90:Flag_Right=Flag_back=Flag_front=0;Flag_Left=1;break;
            case R_90:case T_180:Flag_back=Flag_Left=Flag_front=0;Flag_Right=1;break;
            case Line:Flag_back=Flag_Left=Flag_Right=0;Flag_front=1;break;
        }
    }

}

/*转向延时时间 停止前延时时间*/
void Callback_car(void *argument)
{
    osSemaphoreRelease(myBinarySem_carHandle);
    stopBeepAcktion(BEEP_LOOP);
    if(app.Control_flag==AUTO){
        if(app.state==Cross){
            if(app.Turn_mode==01)app.state=L_90;
            else if(app.Turn_mode==10)app.state=R_90;
            app.Turn_mode=0;
        }
        else if(app.state==Corner)app.state=T_180;
        else app.state=Line;   //转弯结束之后，应该及时巡线调正自己位置
    }
    else if(app.Control_flag==MANUAL){
        // 在手动模式下，设置成巡线模式，（最终状态决定于识别的东西，进入哪种模式）
        // 实现识别到路口之后停止，正好停在路口中央
        // 实现识别到尽头之后停止，正好停止道路尽头
        app.state=Wait;
    }
}


